import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { CmtContentLikeApi } from '#/api/cmt/content/like';

import { getBaseCommunityOptions } from '#/api/cmt/base/community';
import { getMerchantInfoOptions } from '#/api/cmt/base/merchantInfo';
import { getRangePickerDefaultProps } from '#/utils';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'communityId',
      label: '社区',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择社区',
        api: getBaseCommunityOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'merchantId',
      label: '商户',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择商户',
        api: getMerchantInfoOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'targetType',
      label: '目标类型',
      rules: 'required',
      component: 'Select',
      formItemClass: 'col-span-1',
      componentProps: {
        options: [
          { label: '帖子', value: 'post' },
          { label: '评论', value: 'comment' },
          { label: '活动', value: 'activity' },
          { label: '课程', value: 'course' },
        ],
        placeholder: '请选择点赞目标类型',
      },
    },
    {
      fieldName: 'targetId',
      label: '目标ID',
      rules: 'required',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入点赞目标ID',
      },
    },
    {
      fieldName: 'userId',
      label: '点赞会员',
      rules: 'required',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入点赞会员ID',
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'targetType',
      label: '目标类型',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: [
          { label: '帖子', value: 'post' },
          { label: '评论', value: 'comment' },
          { label: '活动', value: 'activity' },
          { label: '课程', value: 'course' },
        ],
        placeholder: '请选择目标类型',
      },
    },
    {
      fieldName: 'targetId',
      label: '目标ID',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入目标ID',
      },
    },
    {
      fieldName: 'userId',
      label: '点赞会员',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入点赞会员ID',
      },
    },
    {
      fieldName: 'communityId',
      label: '社区ID',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入社区ID',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        clearable: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<CmtContentLikeApi.Like>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '点赞ID',
      minWidth: 100,
    },
    {
      field: 'targetType',
      title: '目标类型',
      minWidth: 120,
      slots: { default: 'targetType' },
    },
    {
      field: 'targetId',
      title: '目标ID',
      minWidth: 120,
    },
    {
      field: 'userId',
      title: '点赞会员',
      minWidth: 120,
    },
    {
      field: 'communityId',
      title: '社区ID',
      minWidth: 120,
    },
    {
      field: 'merchantId',
      title: '商户ID',
      minWidth: 120,
    },
    {
      field: 'createTime',
      title: '点赞时间',
      minWidth: 160,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
